package com.hoshiibuy.platform.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.platform.entity.OrganizationType;
import com.hoshiibuy.platform.entity.param.OrganizationTypeInsertParam;
import com.hoshiibuy.platform.entity.param.OrganizationTypeUpdateParam;
import com.hoshiibuy.platform.entity.param.PlatFormParam;
import com.hoshiibuy.platform.service.OrganizationTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 机构类型 前端控制器
 * </p>
 *
 * @author yl
 * @since 2019-04-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/organizationType")
@Api(value = "平台机构类型管理", tags = {"平台机构类型操作接口"})
public class OrganizationTypeController {

    @Autowired
    private OrganizationTypeService organizationTypeService;

    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 分页查询平台机构类型信息
     */
    @ApiOperation(value = "查询平台机构类型记录")
    @PostMapping("/query/list")
    public Response list(@RequestBody @Valid PlatFormParam param) {
        try {
            log.info("查询平台机构类型信息");

            QueryWrapper<OrganizationType> queryWrapper = new QueryWrapper<>();

            return Response.data(organizationTypeService.list(queryWrapper));

        } catch (ServiceException e) {
            log.error("查询平台机构类型信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("查询平台机构类型信息异常", e);
            return Response.failed("查询平台机构类型信息异常");

        }

    }

    /**
     * @param id
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据id查询平台机构类型信息
     */
    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询平台机构类型信息")
    public Response queryOrganizationTypeById(Long id) {
        log.info("查询平台机构类型信息");
        return Response.data(organizationTypeService.getById(id));
    }


    /**
     * @param organizationTypeInsertParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台机构类型信息")
    public Response saveOrganizationType(@RequestBody @Valid OrganizationTypeInsertParam organizationTypeInsertParam) {
        try {

            OrganizationType organizationType = BeanUtils.covertType(organizationTypeInsertParam, OrganizationType.class);
            return Response.data(organizationTypeService.save(organizationType));

        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }

    }

    /**
     * @param organizationTypeUpdateParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台机构类型信息")
    public Response updateOrganizationTypeById(@RequestBody @Valid OrganizationTypeUpdateParam organizationTypeUpdateParam) {
        try {
            OrganizationType organizationType = BeanUtils.covertType(organizationTypeUpdateParam, OrganizationType.class);
            return Response.data(organizationTypeService.updateById(organizationType));

        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }

    }

}

